package com.max.dao;

import com.max.base.BaseDao;
import com.max.entity.UserMessage;
import com.max.utils.CommonUtils;

import java.util.List;

public class UserMessageDao extends BaseDao<UserMessage> {

    private final static UserMessageDao instance = new UserMessageDao();

    private UserMessageDao() {
    }

    public static UserMessageDao getInstance() {
        return instance;
    }


    @Override
    public int insert(UserMessage entity) {
        return super.actionExecute(
                "insert into user_message(message_content, username) " +
                        "values(?, ?)",
                new Object[]{entity.getMessageContent(), entity.getUsername()}
        );
    }

    @Override
    public int updateById(UserMessage entity) {
        return 0;
    }

    @Override
    public int deleteById(Long id) {
        return super.actionExecute(
                "delete from user_message where id = ?",
                new Object[]{id}
        );
    }

    @Override
    public UserMessage queryById(Long id) {
        List<UserMessage> userMessages = super.queryExecute(
                UserMessage.class,
                "select * from user_message where id = ?",
                new Object[]{id}
        );
        return userMessages.isEmpty() ? null : userMessages.get(0);
    }

    @Override
    public List<UserMessage> selectList(UserMessage entity) {
        return super.queryExecute(
                UserMessage.class,
                "select * from user_message where 1=1 "
                        + (CommonUtils.isNotBlank(entity.getUsername()) ? "and username like concat('%', ?, '%') " : "")
                        + (CommonUtils.isNotBlank(entity.getMessageContent()) ? "and message_content like concat('%', ?, '%') " : ""),
                new Object[]{entity.getUsername(), entity.getMessageContent()}
        );
    }

    public List<UserMessage> selectMessageListOrderByTime() {
        return super.queryExecute(
                UserMessage.class,
                "select * from user_message order by create_time desc",
                 new Object[]{}
        );
    }
}
